package com.oa.ylms.apps.business.goods.impl;

import java.util.List;

import com.oa.ylms.apps.business.goods.IGoodsCategoryService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.dao.goods.IGoodsCategoryDao;
import com.oa.ylms.apps.entity.goods.GoodCategoryValue;

public class CategoryServiceImpl implements IGoodsCategoryService {

	private IGoodsCategoryDao goodsCateDao;
	private GoodCategoryValue goodCategory;

	// Service method

	public List<GoodCategoryValue> queryAll(int usability) throws Exception {
		return goodsCateDao.queryAll(usability);
	}


	@Override
	public boolean modifyGoodsCategory(Integer categoryId, String categoryName,
			int states) throws Exception {
		goodCategory = queryByCateId(categoryId);
		goodCategory.setCategoryName(categoryName);
		goodCategory.setStates(states);
		return goodsCateDao.editGoodsCategory(goodCategory);
	}

	@Override
	public boolean deleteGoodsCategory(Integer categoryId) throws Exception{
		goodCategory = queryByCateId(categoryId);
		goodCategory.setStates(IDEDStates.DELETE);
		return goodsCateDao.editGoodsCategory(goodCategory);
	}

	@Override
	public String deleteGoodsCategorys(String categoryIds) throws Exception {
		String unDelIds = "";
		String[] idArr = categoryIds.split(",");
		for (String idStr : idArr) {
			int categoryId = Integer.parseInt(idStr);
			goodCategory = queryByCateId(categoryId);
			goodCategory.setStates(IDEDStates.DELETE);
			boolean result = goodsCateDao.editGoodsCategory(goodCategory);
			if (result == false) {
				unDelIds = categoryId + ",";
				continue;
			}
		}
		return unDelIds.substring(0, unDelIds.length() - 1);
	}

	@Override
	public int addGoodsCategory(String categoryName) throws Exception {
		goodCategory.setCategoryName(categoryName);
		goodCategory.setStates(IDEDStates.ENABLE);
		return goodsCateDao.addGoodsCategory(goodCategory);

	}

	public GoodCategoryValue queryByCateId(Integer categoryId) {
		goodCategory.setCategoryId(categoryId);
		return goodsCateDao.queryByCateId(goodCategory);
	}
	@Override
	public boolean toDisable(int categoryId) throws Exception {
		goodCategory=queryByCateId(categoryId);
		goodCategory.setStates(IDEDStates.DISABLE);
		return goodsCateDao.editGoodsCategory(goodCategory);
	}

	@Override
	public boolean toEnable(int categoryId) throws Exception {
		goodCategory=queryByCateId(categoryId);
		goodCategory.setStates(IDEDStates.ENABLE);
		return goodsCateDao.editGoodsCategory(goodCategory);
	}

	public IGoodsCategoryDao getGoodsCateDao() {
		return goodsCateDao;
	}

	public void setGoodsCateDao(IGoodsCategoryDao goodsCateDao) {
		this.goodsCateDao = goodsCateDao;
	}

	public GoodCategoryValue getGoodCategory() {
		return goodCategory;
	}

	public void setGoodCategory(GoodCategoryValue goodCategory) {
		this.goodCategory = goodCategory;
	}






}
